import React from 'react';
import { connect } from 'dva';
import { Link, browserHistory } from 'dva/router';
import { Table, Row, Col, Form, Input, Radio, Select, Button, message, Popconfirm } from 'antd';
const R = require('ramda');
import RightBox from '../../../components/commons/RightBox';
import RightConstants from '../../../utils/RightConstants';
import Constants from '../../../utils/Constants';
import BizParams from '../../../components/commons/BizParams';
import VipCustomerDetail from '../../../components/vip/VipCustomerDetail';
import VipSendTaxInfo from '../../../components/vip/VipSendTaxInfo';
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
const TRANSFER = ["未转移", "已转移"];
const IE_AUTH = ["没有", "有"];
const INVOICE_STATUS = ["未邮寄", "已邮寄", "不需要"];
export default connect(({ vipManage }) => ({ vipManage }))(
    Form.create()(class VipApproval extends React.Component {
        constructor(props) {
            super(props)
        }
        componentDidMount() {
            this.props.dispatch({ type: 'vipManage/queryGoods' });//查询产品列表
            this.props.dispatch({
                type: 'vipManage/queryRegCustomers',
                payload: {
                    status: 1,
                }
            });
        }
        handleView(record) {
            this.props.dispatch({
                type: 'vipManage/getRegCustomerById',
                payload: {
                    id: record.id,
                }
            });
            this.vipCustomerDetail.handleOpen(true, null, record, "approval")
        }
        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.vipManage.page;
            newPage.page = 1;
            this.props.dispatch({
                type: 'vipManage/queryRegCustomers',
                payload: {
                    cstName: values.cstName,
                    cstType: values.cstType,
                    vipType: values.vipType,
                    waiter: values.waiter,
                    status: 1,
                    page: newPage
                }
            });
        };

        collectSearchCondition = () => {
            let conditions = this.props.form.getFieldsValue();
            conditions.page = this.props.vipManage.page;
            return conditions;
        };

        columns = [
            { title: '客户名称', dataIndex: 'cstName', key: 'cstName' },
            { title: '客户类型', dataIndex: 'cstTypeValue', key: 'cstTypeValue' },
            { title: '会员类型', dataIndex: 'vipTypeValue', key: 'vipTypeValue' },
            {
                title: '转移状态', dataIndex: 'transfer', key: 'transfer',
                render: (text, record, index) => {
                    return (TRANSFER[record.transfer])
                }
            },
            {
                title: '进出口权', dataIndex: 'ieRights', key: 'ieRights',
                render: (text, record, index) => {
                    return (IE_AUTH[record.ieRights])
                }
            },
            {
                title: '发票状态', dataIndex: 'invoiceStatus', key: 'invoiceStatus',
                render: (text, record, index) => {
                    return (R.isNil(INVOICE_STATUS[record.invoiceStatus]) ? INVOICE_STATUS[2] : INVOICE_STATUS[record.invoiceStatus])
                }
            },
            { title: '业务经理', dataIndex: 'waiterName', key: 'waiterName', },
            {
                title: '客户状态', dataIndex: 'status', key: 'status',
                render: (text, record, index) => {
                    return (Constants.VIP_CUSTOMER_STATUS[record.status])
                }
            },
            {
                title: '操作', key: 'oprater',
                render: (text, record, index) => {
                    let approvalLink = <a href="#" onClick={this.handleView.bind(this, record)}>审核</a>;
                    return (<span>{approvalLink}</span>);
                }
            }
        ];

        render() {
            let dataSource = R.isNil(this.props.vipManage.list) ? {} : this.props.vipManage.list;
            let formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                current: this.props.vipManage.page.page,
                pageSize: this.props.vipManage.page.size,
                total: this.props.vipManage.page.totalRow,
                showSizeChanger: true,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.vipManage.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'vipManage/queryRegCustomers',
                        payload: {
                            cstName: values.cstName,
                            cstType: values.cstType,
                            vipType: values.vipType,
                            waiter: values.waiter,
                            status: 1,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.vipManage.page;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'vipManage/queryRegCustomers',
                        payload: {
                            cstName: values.cstName,
                            cstType: values.cstType,
                            vipType: values.vipType,
                            waiter: values.waiter,
                            status: 1,
                            page: newPage
                        }
                    });
                }
            }
            let statusArray = [<Radio value="" key={"all"}>全部</Radio>];
            R.mapObjIndexed((value, key) => {
                if (key !== "0") statusArray.push(<Radio value={key} key={key}>{value}</Radio>);
            }, Constants.VIP_CUSTOMER_STATUS);
            return (
                <div>
                    <Row>
                        <Col span={6}><FormItem {...formItemLayout} label={'客户名称'}>{getFieldDecorator('cstName')(<Input />)}</FormItem></Col>
                        <Col span={6}><FormItem {...formItemLayout} label={'客户类型'}>
                            {getFieldDecorator('cstType')(
                                <BizParams sprCode={"cstType"} style={{ width: '100%' }} all={true} />
                            )}
                        </FormItem></Col>
                    </Row>
                    <Row>
                        <Col span={6}><FormItem {...formItemLayout} label={'会员类型'}>
                            {getFieldDecorator('vipType')(
                                <Select style={{ width: '100%' }} disabled={this.props.disabled} showSearch placeholder="可通过输入关键字搜索" filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                    <Select.Option value={""} key="all">全部</Select.Option>
                                    {
                                        !R.isNil(this.props.vipManage.goodsList) ? this.props.vipManage.goodsList.map(item => {
                                            return (<Select.Option value={item.id + ""} key={item.id + ""}>{item.name}</Select.Option>);
                                        }) : ''
                                    }
                                </Select>
                            )}
                        </FormItem></Col>
                        <Col span={6} style={{ float: 'right' }}>
                            <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>
                    </Row>
                    <Table rowKey={record => record.id} dataSource={dataSource} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                    <VipCustomerDetail wrappedComponentRef={(inst) => this.vipCustomerDetail = inst} dataSource={this.props.vipManage.dataSource} dispatch={this.props.dispatch} goodsList={this.props.vipManage.goodsList} />
                    <VipSendTaxInfo wrappedComponentRef={(inst) => this.vipSendTaxInfo = inst} dispatch={this.props.dispatch} />
                </div>
            )
        }


    })

)